package nl.twleaks.alice.task;

import java.io.IOException;

import nl.twleaks.alice.services.RequestService;

import com.google.appengine.api.urlfetch.HTTPRequest;
import com.google.appengine.api.urlfetch.HTTPResponse;

public class TaskExecutor {
	public static void execute(Task task){
		System.out.println("Executing task " + task.getClass().getSimpleName());
		
		task.beforeExecute();
		if(task instanceof RequestTask) {
			HTTPRequest request = ((RequestTask) task).getRequest();
			HTTPResponse response;
			try {
				response = RequestService.request(request);
				((RequestTask) task).setResponse(response);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		task.afterExecute();
		task.afterTask();
		
		if(task instanceof BasicTask) {
			int time = ((BasicTask) task).getWait();
			if(time > 0) {
				TaskWaitlist.waitForNewTask(time);
			}
		}
	}
}
